var EumPkGroup = require('../../Enum/EumPkGroup');
var PkCardTool = require('../../FindTool/PkCardTool');
var AIGetOne = require('./../Get/AIGetOne');
var AIGetPairs = require('./../Get/AIGetPairs');
var AIGetThree = require('./../Get/AIGetThree');
var AIGetShunZi = require('./../Get/AIGetShunZi');
var AIGetBomb = require('./../Get/AIGetBomb');
var AIGetBombKing = require('./../Get/AIGetBombKing');
var FindShunZiTool = require('../../FindTool/FindShunZiTool');


var FindSameCardValueTool = require('../../FindTool/FindSameCardValueTool');
var _ = require('../../../FrameWork/we_underscore');

/**ai要牌 */
var AITip = module.exports = {};


/***AI出牌
 * _cardIds:[1,23,3];
 * _lastCardInfo:{type:1,cardIds:[1,2,3,4]}
 * return  {
                type: findType,//类型
                result: [{cardId:1,value:1}]//要的起得牌
            }
 */
AITip.doAI = function(_cardIds, _lastCardInfo) {
    var _type = _lastCardInfo.type;
    var resultTmp = {
        isFind: false,
        findGroupMore: [], //多组
    };
    /**上家出的是王炸别不可能要得起 */
    if (_type == EumPkGroup.BOMBKING) {
        return resultTmp;
    }
    //-----------------------------------------------------------
    var _cardsInfo = PkCardTool.getCardInfos(_cardIds);
    _cardsInfo = _.sortBy(_cardsInfo, function(_cardInfo) {
        return -_cardInfo.value;
    });
    _lastCardInfo.cardsInfo = PkCardTool.getCardInfos(_lastCardInfo.cardIds);

    var types = this.getCanGetTypes(_type);
    var typeSize = _.size(types);




    for (var i = 0; i < typeSize; ++i) {
        var findType = types[i];
        if (EumPkGroup.ONE == findType) {
            var _result = AIGetOne.doMore(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: [_cardInfo]
                    });
                });
            }
        } else if (EumPkGroup.PAIRS == findType) {
            var _result = AIGetPairs.doMore(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardsInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: _cardsInfo
                    });
                });
            }
        } else if (EumPkGroup.THREE == findType) {
            var _result = AIGetThree.doMore(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardsInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: _cardsInfo
                    });
                });
            }
        } else if (EumPkGroup.THREEONE == findType) {
            var _result = AIGetThree.doBeltOneMore(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardsInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: _cardsInfo
                    });
                });
            }
        } else if (EumPkGroup.THREEPAIRS == findType) {
            var _result = AIGetThree.doBeltPairsMore(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardsInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: _cardsInfo
                    });
                });
            }
        } else if (EumPkGroup.STRAIGHT == findType) {
            var _result = AIGetShunZi.doMore(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardsInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: _cardsInfo
                    });
                });
            }
        } else if (EumPkGroup.BOMB == findType) {
            var _result = AIGetBomb.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                _.each(_result.allGetCardsGroup, function(_cardsInfo) {
                    resultTmp.findGroupMore.push({
                        type: findType,
                        cardsInfo: _cardsInfo
                    });
                });
            }
        } else if (EumPkGroup.BOMBKING == findType) {
            var _result = AIGetBombKing.do(_lastCardInfo, _cardsInfo);
            if (_result.isFind) {
                resultTmp.findGroupMore.push({
                    type: findType,
                    cardsInfo: _result.recommendCards
                });
            }
        }
    }
    resultTmp.isFind = _.size(resultTmp.findGroupMore) > 0;
    return resultTmp;
};

/***要得起的牌型
 * _type:
 */
AITip.getCanGetTypes = function(_type) {
    return [_type, EumPkGroup.BOMB, EumPkGroup.BOMBKING];
};